package com.fypWebService;

import java.io.IOException;
import java.util.Hashtable;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AccountEnquiry
 */
public class Login2FA2Action extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login2FA2Action() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String hash2FA = request.getParameter("hash2FA");
		String userName= request.getParameter("userName");
		
		System.out.println("<fypWebService><Login2FA2Action>userName= "+userName);
				
		SSOClientService ssocs = new SSOClientService();
		
		
		System.out.println("hash2FA: "+hash2FA);
		
		
		//change this later
		SSOData ssoData=ssocs.login2FA2(userName,hash2FA);
		
		
		if(ssoData.getLogin2FA2Success()){
			System.out.println("<fypWebService><Login2FA2Action>cookie= "+ssoData.getCookieHash());
			Cookie cookie1 = new Cookie("fypUserHash",ssoData.getCookieHash());
			cookie1.setMaxAge(60*5);
			System.out.println("<fypWebService><Login2FA2Action>cookieHash= "+ssoData.getCookieHash());
			response.addCookie(cookie1);
			
			
			request.setAttribute("userName", userName);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/Welcome.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}else{
			RequestDispatcher dispatcher = request.getRequestDispatcher("/LoginFailed.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
			
	}

}
